package com.linyaonan.leetcode.easy._06;

import java.util.ArrayList;
import java.util.Collections;

/**
 * @author: Lin
 * @date: 2020/3/30
 */
public class ReversePrint {
    public int[] reversePrint(ListNode head) {
        if (head == null) {
            return new int[]{};
        }
        ArrayList<Integer> add = new ArrayList<>();
        while (head != null) {
            add.add(head.val);
            head = head.next;
        }
        Collections.reverse(add);
        int[] ints = new int[add.size()];
        for (int i = 0; i < add.size(); i++) {
            ints[i] = add.get(i);
        }
        return ints;
    }

    public int[] reversePrint2(ListNode head) {
        if (head == null) {
            return new int[]{};
        }
        // 链表长度统计
        int count = 0;
        ListNode cur1 = head;
        ListNode cur2 = head;
        while (head != null) {
            count++;
            head = head.next;
        }
        int[] ints = new int[count];
        while (cur1 != null) {
            ints[count - 1] = cur1.val;
            cur1 = cur1.next;
            count--;
        }
        return ints;
    }
}
